﻿using System;
using Microsoft.Extensions.Caching.Memory;

namespace WxOpenPlatLibCore.Utils
{
    public class CacheHelper
    {
        private readonly static MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

        /// <summary>
        /// 创建缓存项的文件
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">object对象</param>
        public static void Insert(string key, object obj)
        {
            //MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
            //创建缓存
            memoryCache.Set(key, obj);
        }
        /// <summary>
        /// 移除缓存项的文件
        /// </summary>
        /// <param name="key">缓存Key</param>
        public static void Remove(string key)
        {
            //MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
            //创建缓存
            memoryCache.Remove(key);
        }
        /// <summary>
        /// 创建缓存项的文件依赖
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">object对象</param>
        /// <param name="fileName">文件绝对路径</param>
        public static void Insert(string key, object obj, string fileName)
        {
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
            //MemoryCache myCache = new MemoryCache(new MemoryCacheOptions() { });
            MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions();
            //PollingFileChangeToken是IChangeToken的实现类，通过轮询监控文件变化
            cacheEntityOps.AddExpirationToken(new Microsoft.Extensions.FileProviders.Physical.PollingFileChangeToken(fileInfo));
            //缓存失效时，回调函数
            cacheEntityOps.RegisterPostEvictionCallback((_key, _value, _reason, _state) => 
            {
                memoryCache.Remove(key);
            });
            //添加缓存，key为文件名，value为文件内容
            memoryCache.Set(key, obj, cacheEntityOps);
        }

        /// <summary>
        /// 创建缓存项过期
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">object对象</param>
        /// <param name="expires">过期时间(分钟)</param>
        public static void Insert(string key, object obj, int expires)
        {
            MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
            memoryCache.Set(key, obj, new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromMinutes(expires) });
        }

        public static void InsertAbsCache(string key, object obj, int expires)
        {
            //MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
            memoryCache.Set(key, obj, TimeSpan.FromMinutes(expires));
            //memoryCache.Set(key, obj, new DateTimeOffset(DateTime.Now.AddMinutes(expires)));
        }

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>object对象</returns>
        public static object Get(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return null;
            }
            //MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
            return memoryCache.Get(key);
        }

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <typeparam name="T">T对象</typeparam>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public static T Get<T>(string key)
        {
            object obj = Get(key);
            return obj == null ? default(T) : (T)obj;
        }
    }
}
